import { createRouter, createWebHashHistory } from 'vue-router'
import LoginView from "../views/LoginView.vue"
import LogonView from "../views/LogonView.vue"
import HomeView from "../views/HomeView.vue"

import AdminBookView from "../views/admin/AdminBookView"
import AddBookView from "../views/admin/AddBookView"

import TakeOverView from "../views/admin/TakeOverView"
import TakeView from "../views/admin/TakeView"

import UserView from "../views/admin/UserView"
import AdminView from "../views/admin/AdminView"

import AdminReqView from "../views/admin/AdminReqView"

import BorrorView from "../views/user/BorrorView.vue"
import ReqView from "../views/user/ReqView.vue"


import store from '@/store';
const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta:{
      requestAuth: false,
    }
  },
  {
    path: '/logon',
    name: 'logon',
    component: LogonView,
    meta:{
      requestAuth: false,
    }
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/adminbook',
    name: 'adminbook',
    component: AdminBookView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/take',
    name: 'take',
    component: TakeView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/takeover',
    name: 'takeover',
    component: TakeOverView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/addbook',
    name: 'addbook',
    component: AddBookView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/user',
    name: 'user',
    component: UserView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/admin',
    name: 'admin',
    component: AdminView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/borror',
    name: 'borror',
    component: BorrorView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/req',
    name: 'req',
    component: ReqView,
    meta:{
      requestAuth: true,
    }
  },
  {
    path: '/adminreq',
    name: 'adminreq',
    component: AdminReqView,
    meta:{
      requestAuth: true,
    }
  },

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 强制跳转首页页面
router.beforeEach((to, from, next) => {
  if (to.meta.requestAuth && !store.state.user.is_login) {
    next({ name: "login" });
  } else {
    next();
  }
})

export default router
